import Router from 'koa-router';
import { 
  getComments, 
  getArticleCommentTree,
  getCommentById, 
  createComment, 
  updateComment, 
  updateCommentsStatus,
  deleteComment,
  deleteCommentsBatch,
  getIPBlacklist,
  addIPToBlacklist
} from '../controllers/comment.controller';
import { authMiddleware } from '../middlewares/auth.middleware';
import { roleMiddleware } from '../middlewares/role.middleware';
import { optionalAuthMiddleware } from '../middlewares/optional-auth.middleware';

const router = new Router();

/**
 * @route GET /api/comments
 * @desc 获取评论列表
 * @access Private (需要管理员或编辑角色)
 */
router.get('/', authMiddleware, roleMiddleware(['admin', 'editor']), getComments);

/**
 * @route GET /api/comments/article/:article_id
 * @desc 获取文章评论树
 * @access Public (可选认证，以获取用户信息)
 */
router.get('/article/:article_id', optionalAuthMiddleware, getArticleCommentTree);

/**
 * @route GET /api/comments/:id
 * @desc 获取评论详情
 * @access Private (需要管理员或编辑角色)
 */
router.get('/:id', authMiddleware, roleMiddleware(['admin', 'editor']), getCommentById);

/**
 * @route POST /api/comments
 * @desc 创建评论
 * @access Public (可选认证，以关联用户)
 */
router.post('/', optionalAuthMiddleware, createComment);

/**
 * @route PUT /api/comments/:id
 * @desc 更新评论
 * @access Private (需要管理员或编辑角色)
 */
router.put('/:id', authMiddleware, roleMiddleware(['admin', 'editor']), updateComment);

/**
 * @route PATCH /api/comments/status
 * @desc 批量更新评论状态
 * @access Private (需要管理员或编辑角色)
 */
router.patch('/status', authMiddleware, roleMiddleware(['admin', 'editor']), updateCommentsStatus);

/**
 * @route DELETE /api/comments/:id
 * @desc 删除评论
 * @access Private (需要管理员或编辑角色)
 */
router.delete('/:id', authMiddleware, roleMiddleware(['admin', 'editor']), deleteComment);

/**
 * @route POST /api/comments/batch/delete
 * @desc 批量删除评论
 * @access Private (需要管理员或编辑角色)
 */
router.post('/batch/delete', authMiddleware, roleMiddleware(['admin', 'editor']), deleteCommentsBatch);

/**
 * @route GET /api/comments/ip/blacklist
 * @desc 获取IP黑名单
 * @access Private (需要管理员或编辑角色)
 */
router.get('/ip/blacklist', authMiddleware, roleMiddleware(['admin', 'editor']), getIPBlacklist);

/**
 * @route POST /api/comments/ip/blacklist
 * @desc 添加IP到黑名单
 * @access Private (需要管理员或编辑角色)
 */
router.post('/ip/blacklist', authMiddleware, roleMiddleware(['admin', 'editor']), addIPToBlacklist);

export default router; 